package com.woniuxy.education.controller;

import com.woniuxy.education.entity.Class;
import com.woniuxy.education.entity.PageResult;
import com.woniuxy.education.entity.Teacher;
import com.woniuxy.education.entity.dto.FormDatas;
import com.woniuxy.education.entity.dto.TUInfo;
import com.woniuxy.education.entity.dto.Tclass;
import com.woniuxy.education.service.GradedeanService;
import com.woniuxy.education.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author author
 * @create 2021/4/6
 */

@CrossOrigin
@RestController
@RequestMapping("/gradedean")
public class GradedeanController {
    @Autowired
    GradedeanService gradedeanService;

    //查找非班主任的教师
    @RequestMapping("/teacher")
    @ResponseBody
    protected ResponseResult<List<String>>  findTeacher(Integer id){
        ResponseResult<List<String>> responseResult = new ResponseResult<>();

        if (gradedeanService.findTeacher(id) != null) {
            responseResult.setCode(200);
            responseResult.setMessage("查找成功");
            responseResult.setData(gradedeanService.findTeacher(id));
            return responseResult;
        } else {
            responseResult.setCode(500);
            responseResult.setMessage("暂无相关教师信息，请先安排授课教师！");
            return responseResult;
        }
    }
    //教师关联班级信息
    @RequestMapping("/tcInfo")
    protected PageResult<Tclass>tcInfo(int page, int size){
        return gradedeanService.findTCInfoByPage(page,size);
    }

    //查询教师带班信息
    @RequestMapping("/teacherIf")
    protected  PageResult<Tclass> teacherIf(int page,int size,String cname,String name){

        return gradedeanService.findTCinfoByIf(page,size,cname,name);
    }
    //班级信息
    @RequestMapping("/classInfo")
    protected  PageResult<Tclass> classInfo(int page,int size){
        return  gradedeanService.findClassInfoByPage(page, size);
    }

    //未分配班主任的班级信息
    @RequestMapping("/noHeadTeacherInfo")
    protected PageResult<Class> noHeadTeacher(int page,int size){

        return gradedeanService.findNoHeadTeacherByPage(page, size);

    }
    //未分文理科的班级信息
    @RequestMapping("/noDivInfo")
    protected PageResult<Class> noDivClass(int page,int size){
        System.out.println(gradedeanService.findNoHeadTeacherByPage(1, 8));
        return gradedeanService.findNoDivByPage(page, size);

    }
    //教师账号信息
    @RequestMapping("/tuInfo")
    protected  PageResult<TUInfo> tuInfo(int page,int size){

        System.out.println(gradedeanService.findTUInfoByPage(1, 8));
        return gradedeanService.findTUInfoByPage(page, size);
    }
    //添加教师账号信息
    @PostMapping("/addAccountInfo")
    @ResponseBody
    protected ResponseResult<String> addAccountInfo(@RequestBody FormDatas formDatas){

        System.out.println(formDatas);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (gradedeanService.addAccountInfo(formDatas)) {
            responseResult.setCode(200);
            responseResult.setMessage("添加成功");
            return responseResult;
        } else {
            responseResult.setCode(500);
            responseResult.setMessage("请检查后重新添加！");
            return responseResult;
        }
    }
    //修改教师密码
    @RequestMapping("/changePsw")
    @ResponseBody
    protected ResponseResult<String> changePsw(String newPassword,Integer u_id){

        System.out.println(u_id);
        System.out.println(newPassword);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (gradedeanService.changePsw(u_id,newPassword)){
            responseResult.setCode(200);
            responseResult.setMessage("修改成功");
            return responseResult;
        } else {
            responseResult.setCode(500);
            responseResult.setMessage("修改失败,请确认账号信息重新修改");
            return responseResult;
        }

    }

    //创建班级
    @RequestMapping("/createClass")
    @ResponseBody
    protected ResponseResult<String> createClass(@RequestBody Class cls) {

        System.out.println(cls);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (gradedeanService.createClass(cls)) {

            responseResult.setCode(200);
            responseResult.setMessage("创建成功");
            return responseResult;

        } else {
            responseResult.setCode(500);
            responseResult.setMessage("创建失败,班级已存在");
            return responseResult;
        }

    }

    //查询班级信息
    @RequestMapping("/teacherInfo")
    @ResponseBody
    protected  PageResult<Tclass> teacherInfo(Integer page,Integer size,String cname){
        return gradedeanService.teacherInfo(page,size,cname);
    }
    //设置班主任
    @RequestMapping("/addHead")
    @ResponseBody
    protected ResponseResult<String> addHead(String name){

        ResponseResult<String> responseResult = new ResponseResult<>();
        if (gradedeanService.addHead(name)) {

            responseResult.setCode(200);
            responseResult.setMessage("设置成功");
            return responseResult;

        } else {
            responseResult.setCode(500);
            responseResult.setMessage("设置失败");
            return responseResult;
        }
    }


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

}
